import type { Prisma } from '@prisma/client'
import db from './db'
import { BrowserWindow } from "electron";

export const findAll = () => {
  return db.todo.findMany()
}

export const findOne = (id: number) => {
  return db.todo.findUnique({
    where: {
      id
    }
  })
}

export const create = (props: Prisma.TodoCreateInput) => {
  return db.todo.create({
    data: props
  })
}

export const remove = (id: number) => {
  return db.todo.delete({
    where: {
      id
    }
  })
}

export const batchRemove = (ids: number[]) => {
  return db.todo.deleteMany({
    where: {
      id: {
        in: ids
      }
    }
  })
}

export const update = (id: number, props: Prisma.TodoUpdateInput) => {
  return db.todo.update({
    where: {
      id
    },
    data: props
  })
}

export const active = async (event: Electron.IpcMainInvokeEvent) => {
  const window = BrowserWindow.fromWebContents(event.sender)
  window?.show()
}

export const services = {
  window: {
    active
  },
  todos: {
    findAll,
    findOne,
    create,
    remove,
    batchRemove,
    update
  }
}

export type Services = typeof services
export default services